<template>
  <scroll-view
    class="supplier-scroll supplier-list"
    scroll-y
    @scrolltolower="loadMore"
    :refresher-enabled="true"
    :refresher-triggered="refreshing"
    @refresherrefresh="onRefresh"
  >
    <!-- 顶部操作栏 -->
    <view class="header">
      <view class="header-action" @click="addSupplier">
        <uni-icons type="plus" size="20" color="#fff"></uni-icons>
        <text class="action-text">添加供货者</text>
      </view>
    </view>

    <!-- Tab切换 -->
    <view class="tabs">
      <view class="tab-item" :class="{ active: activeTab === 'all' }" @click="switchTab('all')">
        <text class="tab-text">全部</text>
      </view>
      <view class="tab-item" :class="{ active: activeTab === 'company' }" @click="switchTab('company')">
        <text class="tab-text">经营主体</text>
      </view>
      <view class="tab-item" :class="{ active: activeTab === 'person' }" @click="switchTab('person')">
        <text class="tab-text">自然人</text>
      </view>
    </view>

    <!-- 搜索框 -->
    <view class="search-container">
      <view class="search-box">
        <uni-icons type="search" size="18" color="#999"></uni-icons>
        <input class="search-input" v-model="searchKey" placeholder="搜索供货者名称" @input="onSearch" />
      </view>
    </view>
    <view class="listContainer">
      <!-- 供货商列表 -->
      <view>
        <view class="supplier-items">
          <view class="supplier-item" v-for="item in supplierList" :key="item.supplierId" @click="viewDetail(item)">
            <view class="item-header">
              <view class="company-tag" :class="item.supplierType === '0' ? 'person' : 'company'">
                <text class="tag-text">{{ item.supplierType === '0' ? '个人' : item.supplierType === '1' ? '合作社' : '农贸市场' }}</text>
              </view>
              <view class="item-actions">
                <uni-icons type="list" size="20" color="#666"></uni-icons>
              </view>
            </view>

            <view class="item-content">
              <text class="company-name">{{ item.supplierName }}</text>
              <text class="company-phone">{{ item.contactPhone || item.contactPerson || '-' }}</text>
              <view class="status-info">
                <text class="status-label">证件状态：</text>
                <text class="status-value" :class="'cert-status-' + item.status">{{ getCertStatusText(item.status) }}</text>
              </view>
            </view>
          </view>
        </view>

        <!-- 空状态 -->
        <view class="empty-state" v-if="supplierList.length === 0 && !loading">
          <image src="/static/images/empty.png" class="empty-image"></image>
          <text class="empty-text">暂无供货者</text>
          <view class="empty-action" @click="addSupplier">
            <uni-icons type="plus" size="16" color="#1890ff"></uni-icons>
            <text class="empty-action-text">添加供货者</text>
          </view>
        </view>

        <!-- 加载更多 -->
        <view class="load-more" v-if="supplierList.length > 0">
          <text class="load-more-text">{{ hasMore ? '上拉加载更多' : '没有更多了' }}</text>
        </view>
      </view>
    </view>
  </scroll-view>
</template>

<script>
import { listSupplier, delSupplier } from '@/api/business/supplier.js';

export default {
  data() {
    return {
      activeTab: 'all',
      searchKey: '',
      supplierList: [],
      pageNum: 1,
      pageSize: 10,
      hasMore: true,
      loading: false,
      refreshing: false,
    };
  },
  onLoad() {
    this.loadSuppliers();
  },
  onShow() {
    // 每次显示页面时都刷新列表，确保数据最新
    // this.onRefresh();
  },
  methods: {
    // 切换Tab
    switchTab(tab) {
      this.activeTab = tab;
      this.pageNum = 1;
      this.supplierList = [];
      this.loadSuppliers();
    },

    // 搜索
    onSearch() {
      this.pageNum = 1;
      this.supplierList = [];
      this.loadSuppliers();
    },

    // 加载供货商列表
    async loadSuppliers() {
      if (this.loading) return;

      this.loading = true;
      try {
        const response = await listSupplier({
          pageNum: this.pageNum,
          pageSize: this.pageSize,
        });
        if (response.code === 200) {
          let list = response.data || response.rows;

          // 根据tab筛选类型 (0=个人, 1=合作社, 2=农贸市场)
          if (this.activeTab === 'company') {
            // 经营主体包括合作社和农贸市场
            list = list.filter((item) => item.supplierType === '1' || item.supplierType === '2');
          } else if (this.activeTab === 'person') {
            // 自然人是个人类型
            list = list.filter((item) => item.supplierType === '0');
          }

          // 根据搜索关键词筛选
          if (this.searchKey) {
            list = list.filter((item) => item.supplierName && item.supplierName.includes(this.searchKey));
          }

          this.supplierList = [...this.supplierList, ...list];
          if (this.supplierList.length === response.total) {
            this.hasMore = false;
          }
          // this.hasMore = false // 因为是全量数据，不需要分页
        }
      } catch (error) {
        console.error('加载供货商列表失败:', error);
        this.supplierList = [];
        uni.showToast({
          title: '加载失败，请重试',
          icon: 'none',
          duration: 2000,
        });
      } finally {
        this.loading = false;
        this.refreshing = false;
      }
    },

    // 下拉刷新
    onRefresh() {
      this.refreshing = true;
      this.hasMore = true;
      this.pageNum = 1;
      this.supplierList = [];
      this.loadSuppliers();
    },

    // 加载更多
    loadMore() {
      console.log('加载更多');
      if (!this.hasMore || this.loading) return;
      this.pageNum++;
      this.loadSuppliers();
    },

    // 添加供货商
    addSupplier() {
      this.needRefresh = true;
      uni.navigateTo({
        url: '/pages/supplier/add',
      });
    },

    // 查看详情
    viewDetail(item) {
      uni.navigateTo({
        url: `/pages/supplier/detail?id=${item.supplierId}`,
      });
    },

    // 获取证件状态文本
    getCertStatusText(status) {
      const statusMap = {
        0: '正常',
        1: '停用',
      };
      return statusMap[status] || '正常';
    },
  },
};
</script>

<style scoped>
page{
  position: relative;
  height: 100vh;
}
.supplier-list {
  background-color: #f5f5f5;
  min-height: 100%;
  display: flex;
  flex-direction: column;
}

/* 顶部操作栏 */
.header {
  background: linear-gradient(135deg, #4a90ff 0%, #5da6ff 100%);
  padding: 20rpx 30rpx;
  display: flex;
  justify-content: flex-end;
  align-items: center;
}

.header-action {
  display: flex;
  align-items: center;
  background-color: rgba(255, 255, 255, 0.2);
  padding: 12rpx 24rpx;
  border-radius: 30rpx;
}

.action-text {
  color: #fff;
  font-size: 28rpx;
  margin-left: 8rpx;
}

/* Tab切换 */
.tabs {
  background-color: #fff;
  display: flex;
  height: 88rpx;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.04);
}

.tab-item {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

.tab-text {
  font-size: 30rpx;
  color: #666;
  transition: all 0.3s;
}

.tab-item.active .tab-text {
  color: #ff9500;
  font-weight: 500;
}

.tab-item.active::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 60rpx;
  height: 4rpx;
  background-color: #ff9500;
}

/* 搜索框 */
.search-container {
  padding: 20rpx;
  background-color: #fff;
}

.search-box {
  display: flex;
  align-items: center;
  background-color: #f5f5f5;
  border-radius: 32rpx;
  padding: 16rpx 30rpx;
}

.search-input {
  flex: 1;
  font-size: 28rpx;
  color: #333;
  margin-left: 16rpx;
}

/* 供货商列表 */
.supplier-scroll {
  position: absolute;
  left: 0;
  top: 0;
  background-color: #f5f5f5;
  height: 100%;
  width: 100%;
}

.supplier-items {
  padding: 20rpx;
}

.supplier-item {
  background-color: #fff;
  border-radius: 16rpx;
  padding: 30rpx;
  margin-bottom: 20rpx;
  box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.04);
}

.item-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20rpx;
}

.company-tag {
  padding: 8rpx 16rpx;
  border-radius: 20rpx;
  font-size: 24rpx;
}

.company-tag.company {
  background-color: #e6f7ff;
  color: #1890ff;
}

.company-tag.person {
  background-color: #f6ffed;
  color: #52c41a;
}

.tag-text {
  font-size: 24rpx;
}

.item-content {
  display: flex;
  flex-direction: column;
}

.company-name {
  font-size: 32rpx;
  color: #333;
  font-weight: 500;
  margin-bottom: 12rpx;
}

.company-phone {
  font-size: 28rpx;
  color: #666;
  margin-bottom: 16rpx;
}

.status-info {
  display: flex;
  align-items: center;
}

.status-label {
  font-size: 26rpx;
  color: #999;
}

.status-value {
  font-size: 26rpx;
  margin-left: 10rpx;
}

.status-value.completed {
  color: #52c41a;
}

.status-value.pending {
  color: #ff9500;
}

.status-value.expired {
  color: #ff4d4f;
}

/* 空状态 */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 100rpx 0;
}

.empty-image {
  width: 200rpx;
  height: 200rpx;
  margin-bottom: 30rpx;
}

.empty-text {
  font-size: 28rpx;
  color: #999;
  margin-bottom: 40rpx;
}

.empty-action {
  display: flex;
  align-items: center;
  padding: 20rpx 40rpx;
  border: 2rpx solid #1890ff;
  border-radius: 40rpx;
}

.empty-action-text {
  color: #1890ff;
  font-size: 28rpx;
  margin-left: 8rpx;
}

/* 加载更多 */
.load-more {
  padding: 30rpx;
  text-align: center;
}

.load-more-text {
  font-size: 26rpx;
  color: #999;
}
</style>
